package demo1.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.util.Arrays;

public class LogAdvice {
    //前置通知
    public void beforeLog(JoinPoint jp){
        Signature sg=jp.getSignature();
        MethodSignature ms= (MethodSignature) sg;
        String name=ms.getName();
        Method m=ms.getMethod();//切入点
        Object[]args=jp.getArgs();//方法参数
        Object target=jp.getTarget();
        System.out.println("xxxxxxx");
        System.out.println("前置通知中，方法名："+name+",参数："+ Arrays.toString(args)+",target:"+target);
    }

    //后置通知
    public void afterLog(JoinPoint jp,Object rv){
        System.out.println("afterLog中，方法返回值："+rv);
    }

    //异常通知
    public void exceptionLog(JoinPoint jp,Exception e){
        System.out.println("异常通知："+e.getMessage());
    }

    //环绕通知
    public Object surroundLog(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("方法执行前。。。");
        long begin=System.currentTimeMillis();
        Object pro=jp.proceed();//执行方法
        long end=System.currentTimeMillis();
        System.out.println("方法执行后，共用时："+(end-begin)+"毫秒");
        return pro;
    }

}
